﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace WebApp
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            this.txtStudNo.Text = ((Label)this.Master.FindControl("lblUserName")).Text;
        
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT   Name, Gender, Class, College, Memo FROM      StudentInfo  WHERE   (StudNo = @StudNo)";
            SqlParameter[] pms = new SqlParameter[1];
            pms[0] = new SqlParameter("@StudNo", ((Label)this.Master.FindControl("lblUserName")).Text);
            SqlDataReader dr = SqlHelper.ExecuteReader(strSql, pms);
            if (dr.Read())
            {
                this.txtName.Text = dr["Name"].ToString();
                this.txtGender.Text = dr["Gender"].ToString();
                this.txtClass.Text = dr["Class"].ToString();
                this.txtCollege.Text = dr["College"].ToString();
                this.txtMemo.Text = dr["Memo"].ToString();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('已成功查询！')", true);

            }
            else
            {               
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('该学生信息不存在！')", true);
            }
            dr.Close();

        }

       

        protected void btnUpdata_Click(object sender, EventArgs e)
        {
            try
            {

                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE  StudentInfo ");
                SqlStringBuilder.AppendFormat("SET     Name = N'{0}', Gender = N'{1}', Class = N'{2}', College = N'{3}', Memo = N'{4}' ", this.txtName.Text, this.txtGender.Text, this.txtClass.Text, this.txtCollege.Text, this.txtMemo.Text);
                SqlStringBuilder.AppendFormat("WHERE   (StudNo = N'{0}') ", this.txtStudNo.Text);

                int iUpd = SqlHelper.ExecuteNonQuery(SqlStringBuilder.ToString());
                if (iUpd > 0)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('更新成功！已更新" + iUpd.ToString() + "条记录！')", true);
                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = "更新失败！请重新编辑……具体原因：" + ex.Message;
            }
        }

      

        protected void btnSource_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT  * FROM     StudentInfo  WHERE   (StudNo = @StudNo)";
            SqlParameter[] pms = new SqlParameter[1];
            pms[0] = new SqlParameter("@StudNo", ((Label)this.Master.FindControl("lblUserName")).Text);
            SqlDataReader dr = SqlHelper.ExecuteReader(strSql, pms);
            this.GridView1.DataSource = dr;
            this.GridView1.DataBind();          

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Theme"] = this.DropDownList1.SelectedValue.ToString();
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] != null)
                this.Theme = Session["Theme"].ToString();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                StringBuilder sb = new StringBuilder(1024);
                sb.Append("INSERT INTO StudentInfo ");
                sb.Append("                (StudNo, Name, Gender,Class, College, Memo) ");
                sb.Append("VALUES   (@StudNo,@Name,@Gender,@Class,@College,@Memo) ");
                SqlParameter[] pms = new SqlParameter[6];
                pms[0] = new SqlParameter("@StudNo", this.txtStudNo.Text);
                pms[1] = new SqlParameter("@Name", this.txtName.Text);
                pms[2] = new SqlParameter("@Gender", this.txtGender.Text);
                pms[3] = new SqlParameter("@Class", this.txtClass.Text);
                pms[4] = new SqlParameter("@College", this.txtCollege.Text);
                pms[5] = new SqlParameter("@Memo", this.txtMemo.Text);
                int intResult = SqlHelper.ExecuteNonQuery(sb.ToString(), pms);
                if (intResult > 0)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('保存成功！')", true);

                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = "添加失败！您的信息已存在，不可重新添加……具体原因：" + ex.Message;

            }
        }
       
    }
}